package com.coder4j.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.coder4j.cache.GroupCache;
import com.coder4j.starter.tools.util.StringUtils;
import com.coder4j.starter.web.TreeNodeVO;
import com.coder4j.system.entity.SysGroupEntity;
import com.coder4j.enums.SysGroupTypeEnum;
import com.coder4j.system.service.SysGroupService;
import com.coder4j.system.vo.SysGroupQuery;
import com.coder4j.system.vo.SysGroupVO;
import com.coder4j.system.vo.SysGroupEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 分组
 * @author keller
 * @date 2024-05-07 18:19:35
 */
@Slf4j
@RestController
@RequestMapping("/sysGroup")
public class SysGroupController {
    @Resource
    private SysGroupService service;

    @Resource
    private HttpServletResponse response;

    @GetMapping("/getTypeList")
    @ApiNote(name="获取分组类型列表",createName = "keller",createTime ="2024-5-9 17:06:54")
    public R getTypeList(){
        return R.success(SysGroupTypeEnum.getTypeList());
    }

    @GetMapping("/getOptions")
    @ApiNote(name="获取分组选择器选项",createName = "keller",createTime ="2024-5-9 17:06:54")
    public R getOptions(String type){
        if(!SysGroupTypeEnum.contains(type)){
            return R.paramError();
        }
        SysGroupQuery query = new SysGroupQuery();
        query.setType(type);
        return R.success(service.getOptionList(query));
    }

    @GetMapping("/getGroupOptions")
    @ApiNote(name="获取分组选择器选项",createName = "keller",createTime ="2024-5-9 17:06:54")
    public R getOptions(Long parentId){
        if(parentId == null){
            return R.paramError();
        }

        SysGroupQuery query = new SysGroupQuery();
        query.setParentId(parentId);
        return R.success(service.getOptionList(query));
    }


    /**
     * 按ID查询分组
     * @param id SysGroup.id
     * @return 分组对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询分组",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R<SysGroupVO> getById( Long id){
        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存分组
     * 有 id 字段，更新；否则新增
     * @param sysGroup SysGroup对象
     * @return SysGroup对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存分组",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R save(@RequestBody SysGroupVO sysGroup){
        if(ObjectUtils.hasEmpty(sysGroup.getType(),sysGroup.getName(),sysGroup.getParentId())){
            return R.paramError();
        }

        service.save(sysGroup);
        GroupCache.initCache();
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询分组列表",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R<BasePage<SysGroupVO>> getList(@RequestBody SysGroupQuery query){
        if(query == null){
            query = new SysGroupQuery();
        }
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出分组列表",createName = "keller",createTime = "2024-05-07 18:19:35")
    public void export(@RequestBody SysGroupQuery query){
        if(query == null){
            query = new SysGroupQuery();
        }
        List<SysGroupEntity> list = service.baseQueryList(query);
                List<SysGroupEO> eoList = new ArrayList<>();
                for (SysGroupEntity entity : list) {
                    SysGroupEO eo = new SysGroupEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"SysGroupList",eoList);
    }


    /**
     * 删除分组
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id SysGroup.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除分组",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R deleteById( Long id){
        service.baseDeleteById(id);
        GroupCache.initCache();
        return R.success();
    }

    @PostMapping("/getTreeSelector")
    @ApiNote(name="查询分组树",createName = "keller",createTime = "2024-04-23 09:40:27")
    public R<TreeNodeVO> getTreeSelector(@RequestBody SysGroupQuery query){
        if(query == null){
            query = new SysGroupQuery();
        }
        if(StringUtils.isEmpty(query.getType()) || !SysGroupTypeEnum.contains(query.getType())){
            return R.paramError();
        }
        return R.success(service.getTreeSelector(query.getType(),query.getId()));
    }



    @PostMapping("/getTagTree")
    @ApiNote(name="查询标签树",createName = "keller",createTime = "2024-04-23 09:40:27")
    public R getTagTree(@RequestBody SysGroupVO vo){
        JSONArray extendJson = vo.getExtendJson();
        if(extendJson == null){
            return R.paramError();
        }

        List<Long> ids = JSONArray.parseArray(vo.getExtendJson().toJSONString(), Long.class);

        return R.success(this.service.baseGetListByIds(ids));
    }
}
